<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    data:Object
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHanlder = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHanlder)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHanlder)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
         title: { text: '设备统计',textStyle: {
                color: '#000'
            } },
        color:['#67C23A','#409EFF','#909399'],
        legend: {
          left: 'center',
          bottom: '10',
          data: ['在线', '离线', '未激活']
        },
        calculable: true,
        series: [
          {
            name: '当前状态',
            type: 'pie',
            //roseType: 'radius',
            radius: [55, 95],
            center: ['50%', '38%'],
            data: [
              { value: this.data.online, name: '在线' },
              { value: this.data.offline, name: '离线' },
              { value: this.data.notactive, name: '未激活' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  },
  watch:{
    data:function () {
        this.initChart();
    }
  }
}
</script>
